<template>
  <div>
    <div class="title">
      用电需量分析
    </div>
    <div id="myChart2" style="width: 100%; height: 300px" />
  </div>
</template>

<script>

export default {
  name: "chart2",
  data () {
    return {
      chart2: null
    }
  },
  mounted() {

    let _this = this
    window.addEventListener("resize", function() {
      if (_this.chart2 == null) {
        _this.chart2 = _this.$echarts.init(document.getElementById('myChart2'))
      }
      _this.chart2.getDom().style.height = ( (document.body.clientHeight - 187) / 2) + "px"
      _this.chart2.resize()
    })

    this.$nextTick(() => {
      this.initChart()
    })
  },

  beforeDestroy() {
    if (!this.chart2) {
      return
    }
    this.chart2.dispose()
    this.chart2 = null
  },

  methods: {

    initChart() {

      let chart = this.$echarts.init(document.getElementById('myChart2'))

      this.chart2 = chart

      // 动态设置高度
      chart.getDom().style.height = ( (document.body.clientHeight - 187) / 2) + "px"

      let option = {
        title: {
          text: 'Referer of a Website',
          subtext: 'Fake Data',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 1048, name: 'Search Engine' },
              { value: 735, name: 'Direct' },
              { value: 580, name: 'Email' },
              { value: 484, name: 'Union Ads' },
              { value: 300, name: 'Video Ads' }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };

      chart.setOption(option)
      chart.resize()
    }
  }
}
</script>

<style scoped>
.title {
  font-size: 20px;
  margin-left: 2%;
  margin-top: 1%;
  z-index: 999;
  color: black;
}
</style>
